package com.cloudspace.yunloan.activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudspace.yunloan.R;
import com.cloudspace.yunloan.constants.Constants;
import com.cloudspace.yunloan.entity.CaseEntity;
import com.cloudspace.yunloan.ext.aq.AQUtility;
import com.cloudspace.yunloan.fragment.CaseDetailFragment;
import com.cloudspace.yunloan.fragment.CaseFinishedFragment;
import com.cloudspace.yunloan.fragment.CaseUnfinishedFragment;
import com.cloudspace.yunloan.global.Res;
import com.cloudspace.yunloan.network.JsonCallBack;
import com.cloudspace.yunloan.network.api.API;
import com.cloudspace.yunloan.update.UpdateAgent;
import com.cloudspace.yunloan.update.UpdateInfoResult;
import com.cloudspace.yunloan.utils.AppPreferences;
import com.cloudspace.yunloan.utils.FileUtil;
import com.cloudspace.yunloan.utils.MMAlert;
import com.cloudspace.yunloan.utils.Utility;
import com.cloudspace.yunloan.yinterface.OnFinishCaseClickInterface;

import java.io.File;

import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends YunloanActivity implements View.OnClickListener {
    private CaseUnfinishedFragment unfinishedFragment;
    private CaseFinishedFragment finishedFragment;
    private CaseDetailFragment caseDetailFragment;

    public final static int INDEX_UNFINISHED = 0;
    public final static int INDEX_FINISHED = 1;

    private RelativeLayout unfinishRootView;
    private RelativeLayout finishRootView;

    private TextView unfinishRootTv;
    private TextView finishRootTv;

    private UpdateAgent mUpdateAgent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_case, true);
        getTitleBar().setSimpleMode(Res.getString(R.string.case_unfinish_list));

        unfinishRootView = findViewById(R.id.left_item1);
        finishRootView = findViewById(R.id.left_item2);

        unfinishRootView.setOnClickListener(this);
        finishRootView.setOnClickListener(this);

        unfinishRootTv = findViewById(R.id.left_tv1);
        finishRootTv = findViewById(R.id.left_tv2);
        select(INDEX_UNFINISHED);

        mUpdateAgent = new UpdateAgent(this);

        AQUtility.postDelayed(new Runnable() {
            @Override
            public void run() {
//                mUpdateAgent.getUpdateinfo(true);
            }
        }, 500);
    }



    @Override
    protected void onResume() {
        super.onResume();
        try {
            //删掉缓存的图片和视频
            FileUtil.deleteDirectory(new File(FileUtil.getAuthPhotoPath()));
            FileUtil.deleteFile(AppPreferences.getString(Constants.VIDEO_PATH));
            AppPreferences.putString(Constants.VIDEO_PATH, "");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void select(int i) {
        //实例事物
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        switch (i) {
            case INDEX_UNFINISHED:
                unfinishRootView.setBackgroundColor(Color.parseColor("#3180EB"));
                finishRootView.setBackgroundColor(Color.parseColor("#32373D"));
                unfinishRootTv.setTextColor(Color.parseColor("#ffffff"));
                finishRootTv.setTextColor(Color.parseColor("#C0C3C7"));

                if (unfinishedFragment == null) {
                    unfinishedFragment = new CaseUnfinishedFragment();
                }
                transaction.replace(R.id.fl_main1_fragment, unfinishedFragment, "unfinishedFragment");
                break;

            case INDEX_FINISHED:
                unfinishRootView.setBackgroundColor(Color.parseColor("#32373D"));
                finishRootView.setBackgroundColor(Color.parseColor("#3180EB"));
                unfinishRootTv.setTextColor(Color.parseColor("#C0C3C7"));
                finishRootTv.setTextColor(Color.parseColor("#ffffff"));

                if (finishedFragment == null) {
                    finishedFragment = new CaseFinishedFragment();
                    finishedFragment.setFinishCaseClickInterface(new OnFinishCaseClickInterface() {
                        @Override
                        public void onCaseClick(CaseEntity entity) {
                            showDetailFragment(entity);
                        }
                    });
                }
                transaction.replace(R.id.fl_main1_fragment, finishedFragment, "finishedFragment");
                break;

            default:
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    private void showDetailFragment(CaseEntity entity){
        getTitleBar().setSimpleMode(Res.getString(R.string.detail));

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        unfinishRootView.setBackgroundColor(Color.parseColor("#32373D"));
        finishRootView.setBackgroundColor(Color.parseColor("#3180EB"));
        unfinishRootTv.setTextColor(Color.parseColor("#C0C3C7"));
        finishRootTv.setTextColor(Color.parseColor("#ffffff"));

        if (caseDetailFragment == null){
            caseDetailFragment = new CaseDetailFragment();
            caseDetailFragment.setData(entity);
        }
        transaction.replace(R.id.fl_main1_fragment, caseDetailFragment, "finishedFragment");
        transaction.commitAllowingStateLoss();
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.left_item1){
            select(INDEX_UNFINISHED);
            getTitleBar().setSimpleMode(Res.getString(R.string.case_unfinish_list));
        }else if (v.getId() == R.id.left_item2){
            select(INDEX_FINISHED);
            getTitleBar().setSimpleMode(Res.getString(R.string.case_finish_list));
        }
    }
}